package com.xaut.outpatientsystem.medicinalManagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xaut.outpatientsystem.medicinalManagement.pojo.Medicine;
import com.xaut.outpatientsystem.medicinalManagement.pojo.Receipt;
import com.xaut.outpatientsystem.medicinalManagement.service.ReceiptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/receipt")
public class ReceiptController {

    @Autowired
    private ReceiptService receiptService;

    @GetMapping("/findReceipt")
    public List<Receipt> getAllRecipt() {
        return receiptService.list();
    }

    @PostMapping("/findReceipt")
    public IPage<Receipt> getAllReceipt(@RequestBody Map<String, Object> searchMap, int page, int size) {
        System.out.println(searchMap.values());
        if (searchMap.get("case_id") == null || searchMap.get("case_id").equals("")) {
            Page<Receipt> page1 = new Page<>(page, size);
            IPage<Receipt> iPage1  = receiptService.page(page1);
            return iPage1;
        }
        System.out.println(searchMap.get("case_id"));
        QueryWrapper<Receipt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("case_id", searchMap.get("case_id"));
        Page<Receipt> pages = new Page<>(page, size);
        IPage<Receipt> iPage = receiptService.page(pages, queryWrapper);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());
        return iPage;
    }

    @RequestMapping("/findById")
    public Receipt findById(int id) {
        return receiptService.getById(id);
    }

    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdateReceipt(@RequestBody Receipt receipt) {
        System.out.println(receipt);
        if(receipt.getTime() == null) {
            Date date = new Date();
            receipt.setTime(date);
        }
        return receiptService.saveOrUpdate(receipt);
    }

    @GetMapping("/delete")
    public void delete(int id) {
        receiptService.removeById(id);
    }

    @GetMapping("/findByCaseId")
    public Receipt findByCaseId(int id) {
        QueryWrapper<Receipt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("case_id", id);
        return receiptService.getOne(queryWrapper);
    }
}
